package cn.zl.web.impl.platform;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.yunbamboos.id.SnowflakeIdGenerator;
import cn.yunbamboos.mybatis.conditions.Conditions;
import cn.yunbamboos.rest.service.anno.RestServiceMapping;
import cn.yunbamboos.rest.service.dto.Page;
import cn.yunbamboos.rest.service.dto.out.BaseOutDTO;
import cn.zl.mapper.PlatformMapper;
import cn.zl.model.Platform;
import cn.zl.web.dto.platform.AddPlatformInDTO;
import cn.zl.web.dto.platform.AddPlatformOutDTO;
import cn.zl.web.dto.platform.DeletePlatformInDTO;
import cn.zl.web.dto.platform.DeletePlatformOutDTO;
import cn.zl.web.dto.platform.QueryPlatformByPageInDTO;
import cn.zl.web.dto.platform.QueryPlatformByPageOutDTO;
import cn.zl.web.dto.platform.UpdatePlatformInDTO;
import cn.zl.web.dto.platform.UpdatePlatformOutDTO;
import cn.zl.web.inter.platform.IPlatformService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service("PlatformServiceImpl")
public class PlatformServiceImpl implements IPlatformService {

    @Resource
    private PlatformMapper platformMapper;
    @Resource
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @RestServiceMapping(url = "/web/platform/queryByPage", name = "分页查询国标级联平台列表服务", in = QueryPlatformByPageInDTO.class, out = QueryPlatformByPageOutDTO.class)
    @Override
    public BaseOutDTO queryByPage(QueryPlatformByPageInDTO in) {
        // 创建分页对象
        Page<Platform> page = new Page<>(in.getPageNo(), in.getPageSize());
        // 添加动态条件
        Conditions where = new Conditions();
        // 查询总数
        int total = platformMapper.countForPage(page, where);
        // 查询当前页码数据
        List<Platform> list = platformMapper.queryForPage(page, where);
        page.setRows(list).setTotal(total);
        return new QueryPlatformByPageOutDTO(page);
    }

    @RestServiceMapping(url = "/web/platform/add", name = "添加新国标级联平台服务", in = AddPlatformInDTO.class, out = AddPlatformOutDTO.class)
    @Override
    public BaseOutDTO add(AddPlatformInDTO in) {
        Platform platform = in.getPlatform();
        platform.setPlatformId(snowflakeIdGenerator.nextIdStr());
        String now = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_PATTERN);
        platform.setUpdateTime(now);
        platform.setCreateTime(now);
        platformMapper.insert(platform);
        return new AddPlatformOutDTO();
    }

    @RestServiceMapping(url = "/web/platform/update", name = "更新国标级联平台服务", in = UpdatePlatformInDTO.class, out = UpdatePlatformOutDTO.class)
    @Override
    public BaseOutDTO update(UpdatePlatformInDTO in) {
        Platform platform = in.getPlatform();
        platformMapper.update(platform);
        return new UpdatePlatformOutDTO();
    }

    @RestServiceMapping(url = "/web/platform/delete", name = "删除国标级联平台服务", in = DeletePlatformInDTO.class, out = DeletePlatformOutDTO.class)
    @Override
    public BaseOutDTO delete(DeletePlatformInDTO in) {
        in.forEach(id -> {
            platformMapper.deleteByPrimaryKey(id);
        });
        return new DeletePlatformOutDTO();
    }
}
